package com.ysd.lis.controller.sys;


import com.ysd.common.Result;
import com.ysd.lis.annotation.LogDetail;
import com.ysd.lis.aspect.OperationType;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.SysIntf;
import com.ysd.lis.entity.SysIntfItem;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.service.SysIntfService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api(tags = "接口管理")
@RestController
public class SysIntfController extends BaseController {

    @Autowired
    SysIntfService sysIntfService;

    @ApiOperation("获取接口信息（不分页)")
    @PostMapping("/api/sys/getIntfList")
    public Result getIntfList(@RequestBody SysIntf sysIntf) {
        return sysIntfService.getIntfList(sysIntf);
    }

    @ApiOperation("添加接口")
    @PostMapping("/api/sys/addIntf")
    @LogDetail(requestParam ="addIntf",operationType = OperationType.INSERT, module = "接口管理",content = "新增接口操作")
    public Result addIntf(@RequestBody SysIntf sysIntf) {
        Result result = new Result();
        try {
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isEmpty(sysUser)) {
                return result.fail("从Redis获取用户数据为NULL: 请检查redis相关配置");
            }
            if (ToolsUtils.isEmpty(sysUser.getOrgId())){
                return result.fail("获取机构ID异常 ： 请给登录用户设置所属机构");
            }
            sysIntf.setOrgId(sysUser.getOrgId());
        } catch (Exception e) {
            // 处理异常情况
            result.fail("从Redis获取用户数据出现异常: 请检查redis相关配置");
        }
        return sysIntfService.addIntf(sysIntf);
    }

    @ApiOperation("修改接口")
    @PostMapping("/api/sys/editIntf")
    @LogDetail(requestParam ="editIntf",operationType = OperationType.UPDATE, module = "接口管理",content = "修改接口操作")
    public Result editIntf(@RequestBody SysIntf sysIntf) {
        return sysIntfService.editIntf(sysIntf);
    }

    @ApiOperation("删除接口")
    @DeleteMapping("/api/sys/deleteIntf/{id}")
    @LogDetail(requestParam ="deleteIntf",operationType = OperationType.DELETE, module = "接口管理",content = "删除接口操作")
    public Result deleteIntf(@PathVariable("id") String id) {
        return sysIntfService.deleteIntf(id);
    }

    @ApiOperation("获取接口子项信息（不分页)")
    @PostMapping("/api/sys/getIntfItemList")
    public Result getIntfItemList(@RequestBody SysIntfItem sysIntfItem) {
        return sysIntfService.getIntfItemList(sysIntfItem);
    }

    @ApiOperation("获取接口子项")
    @PostMapping("/api/sys/getIntfItem")
    public Result getIntfItem(@RequestBody SysIntfItem sysIntfItem) {
        return sysIntfService.getIntfItem(sysIntfItem);
    }

    @ApiOperation("添加接口子项")
    @PostMapping("/api/sys/addIntfItem")
    @LogDetail(requestParam ="addIntfItem",operationType = OperationType.INSERT, module = "接口管理",content = "新增接口子项操作")
    public Result addIntfItem(@RequestBody SysIntfItem sysIntfItem) {
        Result result = new Result();
        try {
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isEmpty(sysUser)) {
                return result.fail("从Redis获取用户数据为NULL: 请检查redis相关配置");
            }
            if (ToolsUtils.isEmpty(sysUser.getOrgId())){
                return result.fail("获取机构ID异常 ： 请给登录用户设置所属机构");
            }
        } catch (Exception e) {
            // 处理异常情况
            result.fail("从Redis获取用户数据出现异常: 请检查redis相关配置");
        }
        return sysIntfService.addIntfItem(sysIntfItem);
    }

    @ApiOperation("修改接口子项")
    @PostMapping("/api/sys/editIntfItem")
    @LogDetail(requestParam ="editIntf",operationType = OperationType.UPDATE, module = "接口管理",content = "修改接口子项操作")
    public Result editIntfItem(@RequestBody SysIntfItem sysIntfItem) {
        return sysIntfService.editIntfItem(sysIntfItem);
    }

    @ApiOperation("保存接口子项配置")
    @PostMapping("/api/sys/saveIntfItemSettings")
    @LogDetail(requestParam ="saveIntfItemSettings",operationType = OperationType.UPDATE, module = "接口管理",content = "保存接口子项配置操作")
    public Result saveIntfItemSettings(@RequestBody SysIntfItem sysIntfItem) {
        return sysIntfService.saveIntfItemSettings(sysIntfItem);
    }

    @ApiOperation("删除接口子项")
    @DeleteMapping("/api/sys/deleteIntfItem/{id}")
    @LogDetail(requestParam ="deleteIntfItem",operationType = OperationType.DELETE, module = "接口管理",content = "删除接口子项操作")
    public Result deleteIntfItem(@PathVariable("id") String id) {
        return sysIntfService.deleteIntfItem(id);
    }


    @ApiOperation("复制接口")
    @PostMapping("/api/sys/copyIntfs")
    @LogDetail(requestParam ="copyIntfs",operationType = OperationType.INSERT, module = "接口管理",content = "复制接口操作")
    public Result copyIntfs(@RequestBody SysIntf sysIntf) {
        Result result = new Result();
        try {
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isEmpty(sysUser)) {
                return result.fail("从Redis获取用户数据为NULL: 请检查redis相关配置");
            }
            if (ToolsUtils.isEmpty(sysUser.getOrgId())){
                return result.fail("获取机构ID异常 ： 请给登录用户设置所属机构");
            }
            sysIntf.setOrgId(sysUser.getOrgId());
        } catch (Exception e) {
            // 处理异常情况
            result.fail("从Redis获取用户数据出现异常: 请检查redis相关配置");
        }
        return sysIntfService.copyIntfs(sysIntf);
    }

    @ApiOperation("复制接口")
    @PostMapping("/api/sys/getSource")
    public Result getSource(@RequestBody SysIntfItem sysIntfItem) {
        return sysIntfService.getSource(sysIntfItem);
    }


}
